package com.moleq.device;

import java.io.UnsupportedEncodingException;
import java.util.Locale;

import jp.co.casio.caios.framework.device.lineprintertools.Encoding;

public class DeviceUtil {

	public static byte[] getEncodedStringByCountry(String str, String sCountry) {
		
		String e_str, charEncode = "ISO-8859-1";
			
		if( sCountry.equals(Locale.JAPANESE.getCountry()) ) {
			charEncode = "MS932";
			e_str = str;
		}
		else if (sCountry.equals(Locale.SIMPLIFIED_CHINESE.getCountry()) ){
			charEncode = "GB2312";
			e_str = str;
		}
		else if (sCountry.equals(Locale.TRADITIONAL_CHINESE.getCountry()) ){
			charEncode = "BIG5";
			e_str = str;
		}
		else 
			e_str = Encoding.UTF8toPC858(str);			// Encode UTF-8 -> PC858 except
		
		// Normalize the display string to character byte array.
		StringBuffer sbuffer = new StringBuffer();
		sbuffer.append(e_str);

		byte[] ldisplay = new byte[20];
		byte[] ls1 = null;
		
		try {
			ls1 = e_str.getBytes(charEncode);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		for( int i = 0; i < ldisplay.length; i++ ) {
			if( i < ls1.length )
				ldisplay[i] = ls1[i];
			else
				ldisplay[i] = ' ';
		}
		
		
		return ls1;
	}	

	public static byte[] getEncodedString(String str, String sLang) {

		String e_str, charEncode = "ISO-8859-1";

		if (sLang.equals(Locale.JAPANESE.getLanguage())) {
			charEncode = "MS932";
			e_str = str;
		} else if (sLang.equals(Locale.SIMPLIFIED_CHINESE.getLanguage())) {
			charEncode = "GB2312";
			e_str = str;
		} else if (sLang.equals(Locale.TRADITIONAL_CHINESE.getLanguage())) {
			charEncode = "BIG5";
			e_str = str;
		} else
			e_str = Encoding.UTF8toPC858(str); // Encode UTF-8 -> PC858 except

		// Normalize the display string to character byte array.
		StringBuffer sbuffer = new StringBuffer();
		sbuffer.append(e_str);

		byte[] ldisplay = new byte[20];
		byte[] ls1 = null;

		try {
			ls1 = e_str.getBytes(charEncode);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < ldisplay.length; i++) {
			if (i < ls1.length)
				ldisplay[i] = ls1[i];
			else
				ldisplay[i] = ' ';
		}

		return ldisplay;
	}

	public static String getStringCommand(String Cmd) {
		String result = "";
		int i, j;

		if (Cmd == null || Cmd.equals(""))
			return result;

		String tmp = "";
		String tmp2 = "";

		i = Cmd.indexOf(" ");
		if (i >= 0) {
			tmp = Cmd.substring(0, i).trim();
		} else {
			tmp = Cmd.trim();
		}

		// tmp2 = tmp;
		i = tmp.indexOf("{");
		j = tmp.indexOf("}");

		while (i >= 0 && j >= 0) {
			tmp2 = tmp.substring(0, i);
			result += tmp2;

			tmp2 = tmp.substring(i + 1, j);
			tmp2 = numberString2Char(tmp2);
			result += tmp2;

			tmp = tmp.substring(j + 1);
			i = tmp.indexOf("{");
			j = tmp.indexOf("}");

		}

		result += tmp;

		return result;
	}

	public static String getEndChar(String Cmd) {
		int i, j, k;
		String result = "";

		if (Cmd == null || Cmd.equals(""))
			return result;

		k = Cmd.indexOf(" ");
		if (k >= 0) {
			Cmd = Cmd.substring(0, k).trim();
			result = Cmd;
		}

		i = Cmd.indexOf("{") + 1;
		j = Cmd.indexOf("}");

		if (j - i > 1) {
			result = Cmd.substring(i, j);
			result = numberString2Char(result);
		}
		return result;
	}

	public static String numberString2Char(String num) {
		String result = "";

		if (isNumeric(num)) {
			byte bEnd = Byte.parseByte(num);
			char c = (char) bEnd;
			result = String.valueOf(c);
		}

		return result;
	}

	public static boolean isNumeric(String str) {

		if (str.equals(""))
			return false;

		for (int i = str.length(); --i >= 0;) {
			int chr = str.charAt(i);
			if (chr < 48 || chr > 57)
				return false;
		}
		return true;
	}
}
